package com.tsingteng.cosfun.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.AppCompatSeekBar;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.tsingteng.cosfun.R;

import java.text.SimpleDateFormat;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/28
 * qq(872179966)
 */

public class TSeekBar extends AppCompatSeekBar {
    Paint mPaint;
    private int mTextColor;
    private float mTextSize;
    private float mTextPadding;
    private String mText;//文字的内容
    private float mTextWidth;
    private long mStartTime;
    private long mEndTime;
    private SimpleDateFormat mFormat;

    public TSeekBar(Context context) {
        this(context, null);
    }

    public TSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TSeekBar, defStyleAttr, 0);
        int n = array.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.TSeekBar_textSize:
                    // 文本的字体大小
                    mTextSize = array.getDimension(attr, 15f);
                    break;
                case R.styleable.TSeekBar_textColor:
                    // 文本的字体颜色
                    mTextColor = array.getColor(attr, Color.WHITE);
                    break;
                case R.styleable.TSeekBar_textPadding:
                    // 文本和thumb之间的padding
                    mTextPadding = array.getDimension(attr, 5f);
                    break;
            }
        }
        array.recycle();
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//设置抗锯齿
        mPaint.setTextSize(mTextSize);//设置文字大小
        mPaint.setColor(mTextColor);//设置文字颜色
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        calTextWH();// 确定文本的宽高
        Rect rectSeek = this.getProgressDrawable().getBounds();

        float textX = rectSeek.width() * getProgress() / 100 - mTextWidth / 2 + getPaddingLeft();
        float textY = getPaddingTop() - mTextPadding;
        canvas.drawText(mText, textX, textY, mPaint);// 画文字
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        invalidate();//监听手势滑动，不断重绘文字和背景图的显示位置
        return super.onTouchEvent(event);
    }

    /**
     * 计算绘制文本的宽高
     */
    public void calTextWH(String mText) {
        this.mText = mText;
        mTextWidth = mPaint.measureText(mText);
        // 左下角为绘制text的基点，如果将文本绘制在进度条之下，那就需要知道文本高度；反之则不需要。
//        float textHeight = fm.bottom - fm.top + fm.leading;
    }


}
